<!DOCTYPE HTML>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<meta charset="utf-8">
<head>
    <th:block th:include="include :: header('新建入库单')"/>
    <th:block th:include="include :: layout-latest-css"/>
    <th:block th:include="include :: ztree-css"/>
</head>
<body class="gray-bg">
<div class="container-div">
    <div class="col-sm-12 search-collapse">
        <form method="post" id="user-form">
            <div class="select-list">
                <ul>
                    <li>
                        <label>单据状态：</label>
                        <select id="status" name="status" th:height="300px">
<!--                            <option value="0">-&#45;&#45;请选择-&#45;&#45;</option>-->
<!--                            <option value="1" selected="selected">入库开单</option>-->
<!--                            <option value="2">已审核</option>-->
<!--                            <option value="3">收货确认</option>-->
<!--                            <option value="4">单据执行</option>-->
<!--                            <option value="5">执行完成</option>-->
<!--                            <option value="6">单据确认</option>-->
<!--                            <option value="7">单据作废</option>-->
<!--                            <option value="8">回传完成</option>-->
                            <option th:each="item:${basTypeList}"
                                    th:value="${item.typeValue}" th:text="${item.typeName}">
                            </option>
                        </select>
                    </li>
                    <li>
                        <label>入库类型：</label>
                    <select name="inType">
                        <option value="">所有</option>
                        <option th:each="item:${basTypeList1}"
                                th:value="${item.typeValue}" th:text="${item.typeName}">
                        </option>
                    </select>
                    </li>
                    <li>
                        <label>单据号：</label>
                        <input type="text" name="stockinId">
                    </li>
<!--                    <li>-->
<!--                        <option value="">请选择</option>-->
<!--                        <option th:each="item:${supplierList}"-->
<!--                                th:value="${item.supplierNo}" th:text="${item.supplierName}">-->
<!--                        </option>-->
<!--                        <lable>供应商：</lable>-->
<!--                        <input id="supplierId" name="supplierId" hidden>-->
<!--                        <input id="supplierName" name="supplierName" placeholder="请点击右侧按钮选择" readonly>-->
<!--                        <a id="selectSupplier" class="btn btn-success" onclick="selectSupplier()" >-->
<!--                            ...-->
<!--                        </a>-->
<!--                    </li>-->
                    <li><label>
                       单据来源：
                    </label>
                        <select id="stockinSource" name="stockinSource" required>
                            <option value="0">自建单入库</option>
                            <option value="1" selected>传单入库</option>
                        </select>
                    </li>

                    <li>
                        <label>建单时间：</label><input type="text" placeholder="请单击选择开始时间：" name="startTime" id="startTime"/>---
                        <input type="text" placeholder="请单击选择结束时间：" name="finishTime" id="finishTime"/>
                    </li>
                    <li>
                        <a class="btn btn-primary btn-rounded btn-sm" onclick="$.table.search()"><i
                                class="fa fa-search"></i>&nbsp;搜索</a>
                        <a class="btn btn-warning btn-rounded btn-sm" onclick="$.form.reset()"><i
                                class="fa fa-refresh"></i>&nbsp;重置</a>
                    </li>
                </ul>
            </div>
        </form>
    </div>
    <div class="btn-group-sm" id="toolbar" role="group">
        <a class="btn btn-success btn-xs " onclick="audit()" shiro:hasPermission="mydo:stockinmanage:audit">
            <i class="fa fa-upload"></i> 单据审核
        </a>
<!--        <a class="btn btn-success btn-xs " onclick="receive()" shiro:hasPermission="mydo:stockinmanage:audit">-->
<!--            <i class="fa fa-upload"></i> 到货确认-->
<!--        </a>-->
        <a class="btn btn-success btn-xs" onclick="receiptSure()" shiro:hasPermission="mydo:stockinmanage:reciptSure">
            <i class="fa fa-download"></i> 单据确认
        </a>
        <a class="btn btn-danger" onclick="receiptCancle()" shiro:hasPermission="mydo:stockinmanage:receiptCancle">
            <i class="fa fa-trash-o"></i> 单据作废
        </a>
        <!--<a class="btn btn-danger" onclick="deleteAll()" shiro:hasPermission="mydo:stockinmanage:remove">-->
        <!--<i class="fa fa-trash-o"></i> 单据删除-->
        <!--</a>-->
        <a class="btn btn-danger" onclick="printProduct()" shiro:hasPermission="mydo:stockinmanage:printProduct">
            <i class="fa fa-trash-o"></i> 打印物资条码
        </a>

    </div>

    <div id="div" style="height:350px" class="col-sm-8 select-table table-striped">
        <table id="bootstrap-table" data-mobile-responsive="true"></table>
    </div>
    <div id="div2" style="height:350px" class="col-sm-8 select-table table-striped">
        <table id="bootstrap-table2" data-mobile-responsive="true"></table>
    </div>

</div>
<div th:include="include :: footer"></div>
<!--<div th:include="include :: LodopFuncs_js"></div>-->
<th:block th:include="include :: LodopFuncs_js" />
<th:block th:include="include :: layout-latest-js"/>
<th:block th:include="include :: ztree-js"/>
<script th:inline="javascript">
    var editFlag = [[${@permission.hasPermi('mydo:stockin:edit')}]];
    var removeFlag = [[${@permission.hasPermi('mydo:stockin:remove')}]];
    var prefix = ctx + "mydo/stockinmanage"

    layui.use('laydate', function () {
        var laydates = layui.laydate;
        laydates.render({
            elem: '#startTime'
            , type: 'datetime',
            /* ,range: '到' */
            format: 'yyyy-MM-dd HH:mm:ss'
        })
    })

    layui.use('laydate', function () {
        var laydates = layui.laydate;
        laydates.render({
            elem: '#finishTime'
            , type: 'datetime',
            /* ,range: '到' */
            format: 'yyyy-MM-dd HH:mm:ss'
        })
    })
    //单据来源
    // $(document).ready(function () {
    //     $.post("/bas/basType/list",{parentId:'08'},function(data){
    //
    //         $("#stockinSource").append("<option value='' selected>全部</option>");
    //         // alert(data.rows[0].inOrOut);
    //         for(var i=0;i<data.total;i++){
    //             $("#stockinSource").append("<option value="+data.rows[i].typeValue+">"+data.rows[i].typeName+"</option>");
    //         }
    //     });
    // });
//子表
    var options2 = {
        url: "",
        updateUrl: ctx + "mydo/stockindetail/edit/{id}",
        singleSelect: true,
        search: false,
        showRefresh: true,
        showColumns: true,
        minimumCountColumns: 2,
        clickToSelect: true,//是否启用点击选中行
        sortName: "assignNum",
        sortOrder: "desc",
        modalName: "入库单据子表",
        method: 'post',
        // height: $("#div").height() / 1,
        striped: true, //是否显示行间隔色
        pagination: true,//显示分页
        sidePagination: "client",
        pageNumber: 1,
        pageSize: 15,
        pageList: [10, 25],
        columns:
            [{
                checkbox: true,
            },
                {
                    field: 'stockinDetailId',
                    title: '子表编号'
                },
                {
                    field: 'stockinGuid',
                    title: '主表guid',
                    visible: false
                },
                {
                    field: 'stockinId',
                    title: '主表编号',
                    sortable: true
                },
                {
                    field: 'lotNo',
                    title: '批号',
                    visible: true
                },

                {
                    field: 'productGuid',
                    title: '物资guid',
                    visible: false
                },
                {
                    field: 'productId',
                    title: '物品编号',
                    align: 'center'
                },
                {
                    field: 'productName',
                    title: '物品名称',
                    align: 'center'
                },
                {
                    field: 'quantity',
                    title: '数量',
                    sortable: true
                },
                // {
                //     field: 'stockStatus',
                //     title: '单位',
                //     visible: true
                //
                // },
                {
                    field: 'assignNum',
                    title: '分配数量',
                    visible: true
                },
                // {
                //     field: 'assignNumX',
                //     title: '实际数量',
                //     visible: true
                // }

            ],
        onLoadSuccess(data) {
            switch (data.code) {
                case  500:
                    options2.url = "";
                    $('#bootstrap-table2').bootstrapTable("removeAll")
                    $.modal.alertWarning("查询出错！");
                    break;
                default:
                    break;
            }
        },
    }
    $('#bootstrap-table2').bootstrapTable(options2)

//主表
    var options = {
        url: "/mydo/stockinview/list",
        detailView: false,//父子表
        search: false,
        singleSelect: true,
        showRefresh: true,
        showColumns: true,
        minimumCountColumns: 2,
        height: $("#div2").height() / 1,
        clickToSelect: true,//是否启用点击选中行
        createUrl: prefix + "/add",
        updateUrl: prefix + "/edit/{id}",
        removeUrl: prefix + "/remove",
        sureUrl: prefix + "/sure",
        auditUrl: prefix + "/audit",
        receiptSureUrl: prefix + "/receiptSure",
        receiptCancleUrl: prefix + "/receiptCancle",
        deleteAllUrl: prefix + "/deleteAll",
        modalName: "入库单",
        columns: [{
            checkbox: true
        },
            {
                field: 'stockinGuid',
                title: '主单guid',
                visible: false
            },
            {
                field: 'stockinId',
                title: '主单id'
            },
            {
                field: 'inType',
                title: '入库类型',
                formatter: function (value, row, index) {
                    if (value == 1) {
                        return "生产入库";
                    }
                }

            },
            {
                field: 'createDate',
                title: '建单时间'
            },
            {
                field: 'createManId',
                title: '建单人id'
            },
            {
                field: 'status',
                title: '单据状态',
                formatter: function (value, row, index) {
                    if (value == 1) {
                        return "入库开单";
                    } else if (value == 2) {
                        return "已审核";
                    } else if (value == 3) {
                        return "收货确认";
                    } else if (value == 4) {
                        return "单据执行";
                    } else if (value == 5) {
                        return "执行完成";
                    } else if (value == 6) {
                        return "单据确认";
                    } else if (value == 7) {
                        return "单据作废";
                    } else if (value == 8) {
                        return "回传完成";
                    }

                }
            },
            {
                field: 'stockinSource',
                title: '单据来源',
                formatter: function (value, row, index) {
                    if (value == 0) {
                        return "自建单入库";
                    } else if (value ==1) {
                        return "传单入库";
                    }

                }
            },
            {
                field: 'finishDate',
                title: '完成时间'
            },


        ],
        //双击显示子表
        onDblClickRow: function (t, e) {
            // initSubTable(index, row, $detail);
            options2.url = ctx + "mydo/stockindetail/list?stockinGuid=" + t.stockinGuid;
            $('#bootstrap-table2').bootstrapTable("refresh", options2)
        }
    }
    $.table.init(options)



    function receiptSure() {
        table.set();
        var rows = $.common.isEmpty(table.options.uniqueId) ? $.table.selectFirstColumns() : $.table.selectColumns(table.options.uniqueId);
        if (rows.length == 0) {
            $.modal.alertWarning("请至少选择一条记录！");
            return;
        }
        if (rows.length > 1) {
            $.modal.alertWarning("只允许选择一条记录！");
            return;
        }
        $.modal.confirm("确认要确认选中的" + rows.length + "条单据吗?", function () {
            var url = table.options.receiptSureUrl;
            var data = {"ids": rows.join()};
            $.operate.submit(url, "post", "json", data);
        });
    }

    function audit() {
        table.set();
        var rows = $.common.isEmpty(table.options.uniqueId) ? $.table.selectFirstColumns() : $.table.selectColumns(table.options.uniqueId);
        if (rows.length == 0) {
            $.modal.alertWarning("请至少选择一条记录！");
            return;
        }
        if (rows.length > 1) {
            $.modal.alertWarning("只允许选择一条记录！");
            return;
        }
        $.modal.confirm("确认要审核选中的" + rows.length + "条单据吗?", function () {
            var url = table.options.auditUrl;
            var data = {"ids": rows.join()};
            $.operate.submit(url, "post", "json", data);
        });
    }


    function receive() {
        var rows = $("#bootstrap-table").bootstrapTable('getSelections');
        if (rows.length == 0) {
            $.modal.alertWarning("请至少选择一条数据");
            return;
        }
        if (rows[0].status == 2) {


            $.ajax({
                "url": "/mydo/stockinmanage/receive?stockinId=" + rows[0].stockinId,
                "type": "GET",
                "dataType": "json",
                "success": function (result) {
                    $.modal.alertWarning(result.msg);
                    $.table.search()

                }
            });
        } else {
            $.modal.alertWarning("单据未审核");
        }
    }

    function updateDetail(id) {
        table.set();
        var url = ctx + "mydo/stockindetail/edit{id}".replace("{id}", id);
        $.modal.open("修改单据明细", url);
    }

    function receiptCancle() {
        table.set();
        var rows = $.common.isEmpty(table.options.uniqueId) ? $.table.selectFirstColumns() : $.table.selectColumns(table.options.uniqueId);
        if (rows.length == 0) {
            $.modal.alertWarning("请至少选择一条记录！");
            return;
        }
        if (rows.length > 1) {
            $.modal.alertWarning("只允许选择一条记录！");
            return;
        }
        $.modal.confirm("确认要作废选中的" + rows.length + "条单据吗?", function () {
            var url = table.options.receiptCancleUrl;
            var data = {"ids": rows.join()};
            $.operate.submit(url, "post", "json", data);
        });
    }


    function printProduct() {

        var checkedItems = $("#bootstrap-table2").bootstrapTable('getSelections');
        if (checkedItems.length == 0) {
            $.modal.alertWarning("请至少选择一条物资");
            return;
        }
        if (checkedItems.length > 1) {
            $.modal.alertWarning("只能选择一条物资");
            return;
        }
        $.post("/mydo/stockinmanage/selectInStock", {
            stockinId: checkedItems[0].stockinId
        }, function (result) {
            if(result.status!=2 && result.status!=3){
                $.modal.alertWarning("只能打印已审核的单据！");

            }else{
                print();
            }
        })
        function print() {


            var cishu = 2;//打印次数
            LODOP = getLodop(document.getElementById('LODOP_OB'), document
                .getElementById('LODOP_EM'));
            LODOP.SET_SHOW_MODE("LANGUAGE", 0);
            LODOP.PRINT_INITA(10, 10, 500, 400, "物资条码");
            LODOP.SET_PRINT_PAGESIZE(0, 1300, 500, "");
            LODOP.SET_PRINT_MODE("AUTO_CLOSE_PREWINDOW", 1);//打印后自动关闭预览窗口(2019-1-30)

            $.post("/mydo/stockinmanage/selectInStock", {
                stockinId: checkedItems[0].stockinId
            }, function (result) {


                LODOP.NewPage();
                // LODOP.ADD_PRINT_BARCODE(11,52,155,74,"128B",checkedItems[0].productId);
                LODOP.ADD_PRINT_BARCODE(20, 250, 120, 120, "QRCode",checkedItems[0].stockinId+","+checkedItems[0].productId);
                LODOP.ADD_PRINT_TEXT(20, 5, 70, 26, "物料编号:");
                LODOP.ADD_PRINT_TEXT(20, 73, 155, 26, checkedItems[0].productId);
                LODOP.ADD_PRINT_TEXT(50, 5, 70, 26, "物料名称:");
                LODOP.ADD_PRINT_TEXT(50, 73, 180, 26, checkedItems[0].productName);
                LODOP.ADD_PRINT_TEXT(80, 5, 70, 26, "批号:");
                LODOP.ADD_PRINT_TEXT(80, 73, 155, 26, checkedItems[0].lotNo);

                // LODOP.ADD_PRINT_TEXT(156, 5, 70, 22, "单位：");
                // LODOP.ADD_PRINT_TEXT(156, 73, 116, 22, "单位");

                //新建打印页面
                // LODOP.PRINT_DESIGN();
                LODOP.PREVIEW();
            }, "json");
        }
    }

    // 选择供应商
    function selectSupplier(){
        var url = "/mydo/stockin/selectSupplier";
        $.modal.open("选择物品信息", url);
    }

    // 关闭供应商框
    function closeSelectSupplier(row){
        $("#supplierId").val(row[0].supplierNo);
        $("#supplierName").val(row[0].supplierName);
    }
</script>
</body>
</html>